Ext.define('carManage.controller.CarInfo', {
    extend: 'Ext.app.Controller',
    views: ['ToolBar','ExpressToolbar', 'carInfo.EditOrAddForm', 'carInfo.GridList', 'carInfo.SearchForm', 'carInfo.EditOrAddWindow', 'carInfo.EditOrAddForm'],
    stores: ['CarInfoStore', 'CarList_store', 'CarTypeStore', 'CompanyStore', 'DepartmentStore', 'UseLogoStore', 'ZiChanStore','StatesOfCarsStore'],
    models: ['OneInAll'],
    init: function() {

        // verify();
        this.control({
            '#add_btn': {
                'click': function() {
                    var win = Ext.widget("editOrAddWindow");
                    win.show();
                }
            },
            '#edit_btn': {
                'click': function() {
                    var grid = Ext.ComponentQuery.query("gridList")[0];
                    var edit_data = grid.getSelectionModel().getSelection();
                    if (edit_data.length === 0) {
                        Ext.Msg.alert("提示", "没有选择任何数据！");
                        return;
                    }
                    var win = Ext.widget("editOrAddWindow");
                    var form = win.down("editOrAddForm");
                    if (edit_data.length === 1) {
                        form.getForm().loadRecord(edit_data[0]);
                        win.show();
                    } else {
                        for (i = 0; i < edit_data.length; i++) {
                            var _win = Ext.widget("editOrAddWindow");
                            var _form = _win.down("editOrAddForm");
                            _form.getForm().loadRecord(edit_data[i]);
                            _win.show();
                        }
                    }
                }
            },
            '#del_btn': {
                'click': function() {
                    var del_data = Ext.ComponentQuery.query("gridList")[0].getSelectionModel().getSelection();
                    if (del_data.length === 0) {
                        Ext.Msg.alert('提示', '请选择需要删除的数据！');
                        return;
                    }
                    Ext.MessageBox.show({
                        title: '提示框',
                        msg: ' 确定删除该条信息吗？注意，数据删除后将无法恢复！ ',
                        buttons: Ext.MessageBox.YESNO,
                        buttonText: {
                            yes: "确定",
                            no: "取消"
                        },
                        fn: showResult,
                        icon: Ext.MessageBox.QUESTION
                    });

                    function showResult(btn) {
                        if (btn === 'yes') {
                            del();
                        }
                    }

                }
            },
            '#export_xls': {
                'click': function() {
                    var obj = {
                        code: Ext.ComponentMgr.get("code_from").getValue(),
                        company: Ext.ComponentMgr.get("company_from").getValue(),
                        department: Ext.ComponentMgr.get("department_from").getValue(),
                        type: Ext.ComponentMgr.get("type_from").getValue(),
                        attributes: Ext.ComponentMgr.get("attributes_from").getValue(),
                        states: Ext.ComponentMgr.get("states_from").getValue()
                    };
                    FormUtils("ExportXls_BasicInformation.action",obj);
                }
            },
            '#query_tbar': {
                'click': function() {
                    var store = Ext.ComponentQuery.query("gridList")[0].getStore();
                    store.getProxy().setExtraParam("code", Ext.ComponentMgr.get("code_from").getValue());
                    store.getProxy().setExtraParam("company", Ext.ComponentMgr.get("company_from").getValue());
                    store.getProxy().setExtraParam("department", Ext.ComponentMgr.get("department_from").getValue());
                    store.getProxy().setExtraParam("type", Ext.ComponentMgr.get("type_from").getValue());
                    store.getProxy().setExtraParam("attributes", Ext.ComponentMgr.get("attributes_from").getValue());
                    store.getProxy().setExtraParam("states", Ext.ComponentMgr.get("states_from").getValue());
                    store.load();
                }
            },
            'button[action=save]': {
                'click': function(btn) {
                    var win = btn.up("editOrAddWindow");
                    var form = win.down("editOrAddForm");
                    if (!form.getForm().isValid()) {
                        Ext.MessageBox.alert("提示", "表单没有填写完整，无法提交！");
                        return;
                    }
                    form.getForm().submit({
                        url: 'BasicInformationAction.action',
                        waitMsg: '数据保存中...',
                        submitEmptyText: false,
                        success: function(c, d) {
                            Ext.MessageBox.alert("操作信息", "成功信息保存！");
                            Ext.ComponentQuery.query("gridList")[0].getStore().load();
                            win.close();
                        },
                        failure: function(c, d) {
                            Ext.MessageBox.show({
                                title: "操作信息",
                                msg: "信息保存出错，请联系管理员！",
                                buttons: Ext.MessageBox.OK,
                                icon: "ext-mb-error"
                            });

                        }
                    });
                }
            },
            'button[action=reset]': {
                'click': function(btn) {
                    var win = btn.up("editOrAddWindow");
                    var form = win.down("editOrAddForm");
                    form.getForm().reset();
                }
            }
        });

        function del() {
            var del_data = Ext.ComponentQuery.query("gridList")[0].getSelectionModel().getSelection();
            if (del_data.length === 0) {
                Ext.Msg.alert('提示', '请选择需要删除的数据！');
                return;
            } else {
                var id_array = [];
                for (var i = 0; i < del_data.length; i++) {
                    id_array[i] = del_data[i].data.id;
                }
                sendRequest(id_array);
            }
        }

        function sendRequest(data) {
            Ext.Ajax.request({
                url: 'DeleteAction.action',
                params: {
                    id: data,
                    type: 'car'
                },
                success: function(response, options) {
                    Ext.Msg.alert('提示', '删除成功！');
                    Ext.ComponentQuery.query("gridList")[0].getStore().reload();
                },
                failure: function(response, options) {
                    Ext.Msg.alert('提示', '删除失败！');
                }
            });
        }

    }
});